package com.zmn.plat.dubbo.impl.extensioncategory;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto.VtDTO;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.manager.common.database.annotation.ReadOnlyConnection;
import com.zmn.plat.common.dto.extensioncategory.ExtensionServCategoryDRO;
import com.zmn.plat.dubbo.interfaces.extensioncategory.ExtensionServCategoryListRemoteService;
import com.zmn.plat.dubbo.utils.DubboConsts;
import com.zmn.plat.model.entity.extension.serv.category.ExtensionServCategory;
import com.zmn.plat.model.entity.extension.serv.category.ExtensionServCategoryQuery;
import com.zmn.plat.model.entity.extension.serv.category.group.ExtensionServCategoryGroupQuery;
import com.zmn.plat.model.vo.extension.serv.category.ExtensionServCategoryAssociationVO;
import com.zmn.plat.model.vo.extension.serv.category.group.ExtensionServCategoryGroupVO;
import com.zmn.plat.services.interfaces.extension.serv.category.ExtensionServCategoryAssociationService;
import com.zmn.plat.services.interfaces.extension.serv.category.ExtensionServCategoryService;
import com.zmn.plat.services.interfaces.extension.serv.category.group.ExtensionServCategoryGroupService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.cglib.beans.BeanCopier;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

import static java.util.Collections.emptyList;
import static java.util.stream.Collectors.toList;
import static org.apache.commons.collections4.CollectionUtils.isEmpty;

/**
 * 描述：推广服务分类dubbo接口实现
 *
 * @author linfeng
 * @version v1.0
 * @since 2020/03/30 10:42
 */
@Slf4j
@Service(version = DubboConsts.INTERFACE_VERSION, validation = "true")
public class ExtensionServCategoryListRemoteServiceImpl implements ExtensionServCategoryListRemoteService {
    private static final String TAG = "推广服务分类 dubbo 接口";

    @Resource
    ExtensionServCategoryService extensionServCategoryService;

    @Resource
    ExtensionServCategoryGroupService extensionServCategoryGroupService;

    @Resource
    ExtensionServCategoryAssociationService extensionServCategoryAssociationService;

    @Override
    @ReadOnlyConnection
    public ResponseDTO<ExtensionServCategoryDRO> getByExtServCategId(Integer id) {
        log.info("ExtensionServCategoryListRemoteServiceImpl.getByExtServCategId 参数为：id:{}", id);
        if (Objects.isNull(id)) {
            return ResponseDTO.fail("id不能为空");
        }
        ExtensionServCategory extensionServCategory = extensionServCategoryService.findByKey(id);
        if (Objects.isNull(extensionServCategory)) {
            return ResponseDTO.fail("数据不存在");
        }
        ExtensionServCategoryDRO dro = new ExtensionServCategoryDRO();
        try {
            BeanCopier copierDro = BeanCopier.create(ExtensionServCategory.class, ExtensionServCategoryDRO.class, false);
            copierDro.copy(extensionServCategory, dro, null);
        } catch (Exception ex) {
            log.error("{}对象:{}拷贝出错", TAG,
                "CopyObjectUtil<ExtensionServCategoryDIO, ExtensionServCategoryQuery>", ex);
            return ResponseDTO.fail("拷贝ExtensionServCategoryDIO对象出错");
        }

        return ResponseDTO.success(dro, GlobalConsts.SUCCESS_MSG);
    }

    @Override
    @ReadOnlyConnection
    public ResponseDTO<List<VtDTO>> listByNameAndStatus(String name, Integer status) {
        log.info("ExtensionServCategoryListRemoteServiceImpl.listByNameAndStatus 参数为：name:{},status:{}", name, status);
        ExtensionServCategoryQuery extensionServCategoryQuery = new ExtensionServCategoryQuery();
        extensionServCategoryQuery.setName(name);
        extensionServCategoryQuery.setStatus(status);

        List<ExtensionServCategory> extensionServCategories = extensionServCategoryService.listByQuery(extensionServCategoryQuery);
        if (isEmpty(extensionServCategories)) {
            return ResponseDTO.success(emptyList(), "无符合条件的数据");
        }

        List<VtDTO> vtList = extensionServCategories.stream()
            .map(extensionCategory -> new VtDTO(extensionCategory.getCategId(), extensionCategory.getName(), extensionCategory.getStatus()))
            .collect(toList());

        return ResponseDTO.success(vtList, GlobalConsts.SUCCESS_MSG);
    }

    @Override
    @ReadOnlyConnection
    public ResponseDTO<List<VtDTO>> listExtCategByExtServCategId(Integer id) {
        log.info("ExtensionServCategoryListRemoteServiceImpl.listExtCategByExtServCategId 参数为：id:{}", id);
        if (Objects.isNull(id)) {
            return ResponseDTO.fail("id不能为空");
        }
        ExtensionServCategoryGroupQuery extensionServCategoryGroupQuery = new ExtensionServCategoryGroupQuery();
        extensionServCategoryGroupQuery.setExtensionServCategId(id);
        List<ExtensionServCategoryGroupVO> extensionServCategoryGroups =
            extensionServCategoryGroupService.listByQuery(extensionServCategoryGroupQuery);
        if (isEmpty(extensionServCategoryGroups)) {
            return ResponseDTO.success(emptyList(), "无符合条件的数据");
        }

        List<VtDTO> vtList =
            extensionServCategoryGroups.stream().map(extensionServCategoryGroupVO -> new VtDTO(extensionServCategoryGroupVO.getCategId(),
                extensionServCategoryGroupVO.getExtensionCategName(), extensionServCategoryGroupVO.getStatus())).collect(toList());

        return ResponseDTO.success(vtList, GlobalConsts.SUCCESS_MSG);
    }

    @Override
    @ReadOnlyConnection
    public ResponseDTO<List<VtDTO>> listCategoryByExtServCategIdAndExtCategId(Integer extServCategId, Integer extCategId) {
        log.info(
            "ExtensionServCategoryListRemoteServiceImpl.listCategoryByExtServCategIdAndExtCategId 参数为：extServCategId:{},extCategId{}",
            extServCategId, extCategId);
        if (Objects.isNull(extServCategId)) {
            return ResponseDTO.fail("extServCategId不能为空");
        }
        if (Objects.isNull(extCategId)) {
            return ResponseDTO.fail("extCategId不能为空");
        }
        List<ExtensionServCategoryAssociationVO> extensionServCategoryAssociationVOS =
            extensionServCategoryAssociationService.listByExtensionServCategoryId(extServCategId, extCategId, null);
        if (isEmpty(extensionServCategoryAssociationVOS)) {
            return ResponseDTO.success(emptyList(), "无符合条件的数据");
        }

        List<
            VtDTO> vtList =
                extensionServCategoryAssociationVOS.stream()
                    .map(extensionServCategoryAssociationVO -> new VtDTO(extensionServCategoryAssociationVO.getCategId(),
                        extensionServCategoryAssociationVO.getCategName(), extensionServCategoryAssociationVO.getStatus()))
                    .collect(toList());

        return ResponseDTO.success(vtList, GlobalConsts.SUCCESS_MSG);
    }
}
